package com.xtaifghy.wugui.util;
import com.xtaifghy.wugui.GameState1;

public class Util {
    /*
     *功能是将一个整数n，转换成长度为l的boolean数组
     *如输入n=3,l=4,则返回bArr={true,true,false,false},与3的二进制0011对应
     *因为数组是从左向右读，二进制数是从右向左读，所以顺序相反
     */
    public static boolean[] intToBArr(int n, int l){
        boolean [] bArr = new boolean[l];
        for (int i = 0; i < l; i++) {
            bArr[i] = ((n&(0x01<<i))>0);//1向左移i位后与n进行&操作，如果大于0，说明n的第i位为1，否则为0
        }
        return bArr;
    }

    public static void intToBArr2(int v, boolean[]bArr){
        for (int i = 0; i < 9; i++) {
            bArr[i] = ((v&(0x01<<i))>0);
        }
    }

    /*
    *将boolean数组，转换成整数
     */
    public static int bArrToInt(boolean[] bArr){
        String s="";
        for (int i = 0; i < bArr.length; i++) {
            if(bArr[i]) s = "1"+s;
            else s = "0"+s;
        }
        return Integer.valueOf(s,2);
    }

    /*
     *将boolean数组转换成0,1数组
     */
    public static int[] bArrToIntArr(boolean[] actionSeq){
        int [] numSeq = new int[actionSeq.length];
        for (int i = 0; i < actionSeq.length; i++) {
            numSeq[i]=actionSeq[i]?1:0;
        }
        return numSeq;
    }

    /*
    *将0,1数组转换成boolean数组
     */
    public static boolean[] intArrToBArr(int[] intArr){
        boolean [] bArr = new boolean[intArr.length];
        for (int i = 0; i < intArr.length; i++) {
            bArr[i] = (intArr[i]==1);
        }
        return bArr;
    }

    public static boolean testActionSeq(boolean[] initState, boolean[] goalState, boolean[]actionSeq){
        boolean[] tempState = initState.clone();
        for (int i = 0; i < actionSeq.length; i++) {
            if(actionSeq[i]) {
                for (int j:GameState1.actions[i]) {
                    tempState[j] = !tempState[j];
                }
            }
        }
        for (int i = 0; i < tempState.length; i++) {
            if(tempState[i]!=goalState[i]) return false;
        }
        return true;
    }

    public static void printBArr(boolean[] bArr){
        for (int i = 0; i < bArr.length; i++) {
            System.out.print(bArr[i]+",");
        }
        System.out.println();
    }
}
